//
//  ChangePhoneNumberViewController.m
//  BabyAndBabyDriver
//
//  Created by 尹宁 on 16/8/22.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "ChangePhoneNumberViewController.h"
#import "ChangeNumberView.h"
#import "UIViewController+HUD.h"
@interface ChangePhoneNumberViewController ()

@property (nonatomic, strong) ChangeNumberView *ChangeNumberView;
@end

@implementation ChangePhoneNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"更换手机";
    [self creatBigView];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)creatBigView
{
    self.ChangeNumberView = [[[NSBundle mainBundle] loadNibNamed:@"ChangeNumberView" owner:nil options:nil] lastObject];
    _ChangeNumberView.frame = CGRectMake(0, 0, 350, KSCREENHEIGHT);
    _ChangeNumberView.PhoneNumberTextField.layer.borderWidth = 1;
    _ChangeNumberView.PhoneNumberTextField.borderStyle = UITextBorderStyleRoundedRect;
    _ChangeNumberView.PhoneNumberTextField.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor lightGrayColor]);
    _ChangeNumberView.VerificationCodeTextField.layer.borderWidth = 1;
    _ChangeNumberView.VerificationCodeTextField.borderStyle = UITextBorderStyleRoundedRect;
    _ChangeNumberView.VerificationCodeTextField.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor lightGrayColor]);
    
    [[_ChangeNumberView.GetVerificationCodeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (_ChangeNumberView.PhoneNumberTextField.text.length == 0 || _ChangeNumberView.PhoneNumberTextField.text.length != 11) {
            [self showHint:@"请输入正确手机号" yOffset:-300];
        } else {
            [self getCodeData];
        }
    }];
    [[_ChangeNumberView.SureButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (_ChangeNumberView.PhoneNumberTextField.text.length == 0 || _ChangeNumberView.PhoneNumberTextField.text.length != 11) {
            [self showHint:@"请输入正确手机号" yOffset:-300];
        } else if (_ChangeNumberView.VerificationCodeTextField.text == 0){
            [self showHint:@"请输入验证码" yOffset:-300];
        } else {
            [self changePhoneNumber];
        }
    }];
    [self.view addSubview:_ChangeNumberView];
    
}

- (void)getCodeData
{
    YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"Auth/sendSms.json" paramDic:@{@"mobile":_ChangeNumberView.PhoneNumberTextField.text} RegisterApiMethod:RequestMethodPost];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSLog(@"%@", request.responseBody);

        //        NSLog(@"%@",dic);
    } failure:^(YTKBaseRequest *request) {
        
    }];
}

- (void)changePhoneNumber
{
    YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"User/phone.json" paramDic:@{@"token":TOKEN, @"username":_ChangeNumberView.PhoneNumberTextField.text,@"smscode":_ChangeNumberView.VerificationCodeTextField.text} RegisterApiMethod:RequestMethodPost];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSLog(@"%@", request.responseBody);
        
        //        NSLog(@"%@",dic);
    } failure:^(YTKBaseRequest *request) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
